package com.itboye.sunsun.person.fragments;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.itboye.sunsun.adapter.AbstractOrderAdapter;
import com.itboye.sunsun.adapter.SendOrderAdapter;
import com.itboye.sunsun.adapter.WaitPayOrderAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.OrderBean;
import com.itboye.sunsun.beans.OrderBean.DetailBean;
import com.itboye.sunsun.beans.OrderBean.InnerDetailBean;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.DebugToast;
import com.itboye.sunsun.utils.DoubleParse;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

public class SendFragment extends BaseOrderFragment {

	// 代发货状态码
	String status = "2";
	private List<InnerDetailBean> dataList = new ArrayList<OrderBean.InnerDetailBean>();
	private Map<String, DetailBean> map;

	@Override
	protected String onGetStatus() {
		return status;
	}

	@Override
	protected BaseAdapter onGetAdapter() {
		return new SendOrderAdapter(App.ctx, dataList);
	}

	@Override
	protected void onPullDownFinish(BaseAdapter adapter, XListView xListView, OrderBean orderBean) {
		dataList.clear();
		addDataToList(dataList, orderBean);
		adapter.notifyDataSetChanged();
	}

	@Override

	protected void onPullUpFinish(BaseAdapter adapter, XListView xListView, OrderBean orderBean) {
		addDataToList(dataList, orderBean);
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onPullDownError(BaseAdapter adapter, XListView xListView, Exception exception, int code,
			String msg) {
	}

	@Override
	protected void onPullUpError(BaseAdapter adapter, XListView xListView, Exception exception, int code, String msg) {
	}

	private void addDataToList(List<InnerDetailBean> dataList, OrderBean orderBean) {
		map = orderBean.getList();
		if (map == null) {
			return;
		}
		Set<Entry<String, DetailBean>> set = map.entrySet();
		if (set == null) {
			return;
		}
		for (Entry<String, DetailBean> entry : set) {

			int count = 0;
			double price = 0;
			if (entry == null || entry.getValue() == null || entry.getKey() == null) {
				continue;
			}
			List<InnerDetailBean> list = entry.getValue().get_items();
			if (list == null) {
				continue;
			}
			for (InnerDetailBean innerDetailBean : list) {
				dataList.add(innerDetailBean);
				int c = (int) DoubleParse.parse(innerDetailBean.getCount(), 0);
				count += c;
				price = price + DoubleParse.parse(innerDetailBean.getPrice(), 0) * c;
			}

			InnerDetailBean detailBean = new InnerDetailBean();
			detailBean.setViewType(2);
			detailBean.setOrderCode(entry.getKey());
			detailBean.setCount(count + "");
			detailBean.setPrice("" + entry.getValue().getPrice());
			detailBean.setPostPrice(entry.getValue().getPostPrice());
			detailBean.setDiscountRadio(entry.getValue().getDiscountMoney());
			dataList.add(detailBean);

		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (map == null) {
			return;
		}
		Set<Entry<String, DetailBean>> set = map.entrySet();
		if (set == null) {
			return;
		}

		for (Entry<String, DetailBean> entry : set) {

			if (entry == null || entry.getValue() == null || entry.getKey() == null) {
				continue;
			}
			if (position != 0) {
				// DebugLog.v("ordercode", entry.getValue().getOrderCode());
				// DebugLog.v("ordercode1", dataList.get(position -
				// 1).getOrderCode());
				if (dataList.get(position - 1).getOrderCode().equals(entry.getValue().getOrderCode())) {

					String goodsid = entry.getValue().getId();
					// Toast.makeText(App.ctx, "haha", 0).show();
					List<InnerDetailBean> listInner = entry.getValue().get_items();
					// DebugToast.show(listInner.size(), 1);
					Bundle bundle = new Bundle();
					bundle.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) listInner);
					Intent intent = new Intent(getActivity(), SendActivity.class);
					intent.putExtras(bundle);

					// DebugToast.show(entry.getValue().getOrderCode(), 0);
					intent.putExtra("goodsid", goodsid);
					intent.putExtra("what", dataList.get(position - 1).getId());
					startActivity(intent);
				}
			}

		}

	}

}
